package com.patrol.logisticsmanage.ui.canteen;

import android.app.AlertDialog;
import android.content.DialogInterface;
import android.os.Bundle;
import android.widget.TextView;

import androidx.annotation.Nullable;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.alibaba.android.arouter.facade.annotation.Autowired;
import com.alibaba.android.arouter.facade.annotation.Route;
import com.patrol.logisticsmanage.R;
import com.patrol.logisticsmanage.net.AppConfig;
import com.patrol.logisticsmanage.net.BaseObserver;
import com.patrol.logisticsmanage.net.BaseReponse;
import com.patrol.logisticsmanage.net.BaseRequest;
import com.patrol.logisticsmanage.net.FoodListInfo;
import com.patrol.logisticsmanage.net.OrderPayInfo;
import com.patrol.logisticsmanage.ui.base.BaseActivity;
import com.patrol.logisticsmanage.ui.canteen.bean.FoodBean;
import com.patrol.logisticsmanage.ui.canteen.bean.PayWay;
import com.patrol.logisticsmanage.ui.canteen.bean.Restaurant;
import com.patrol.logisticsmanage.ui.canteen.utils.GsonUtil;
import com.patrol.logisticsmanage.ui.canteen.utils.ToastUtils;

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;
import rx.android.schedulers.AndroidSchedulers;
import rx.schedulers.Schedulers;

@Route(path = "/app/OrderedActivity")
public class OrderedActivity extends BaseActivity {

    @BindView(R.id.tv_title)
    TextView tvTitle;
    @BindView(R.id.tv_shop_name)
    TextView tvShopName;
    @BindView(R.id.rcy_food)
    RecyclerView rcyFood;
    @BindView(R.id.tv_summary)
    TextView tvSummary;
    @BindView(R.id.rcy_pay_type)
    RecyclerView rcyPayType;
    @BindView(R.id.tv_order)
    TextView tvOrder;

    PayTypeAdapter payTypeAdapter;
    FoodNumAdapter foodNumAdapter;

    @Autowired
    String foods;
    List<FoodBean> foodBeans;
    List<PayWay> payWays;

    BigDecimal account;

    @Autowired
    String restaurant;
    Restaurant restaurantBean;
    List<FoodListInfo> listInfos = new ArrayList<>();

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_ordered);
        ButterKnife.bind(this);

        tvTitle.setText("确认订单");
        foodBeans = GsonUtil.gsonToList(foods,FoodBean.class);
        restaurantBean = GsonUtil.gsonToBean(restaurant,Restaurant.class);
        payWays = new ArrayList<>();

        payTypeAdapter = new PayTypeAdapter(mContext,payWays);
        foodNumAdapter = new FoodNumAdapter(mContext,foodBeans);
        rcyFood.setLayoutManager(new LinearLayoutManager(this,LinearLayoutManager.VERTICAL,false));
        rcyPayType.setLayoutManager(new LinearLayoutManager(this,LinearLayoutManager.VERTICAL,false));

        rcyFood.setAdapter(foodNumAdapter);
        rcyPayType.setAdapter(payTypeAdapter);

       account = BigDecimal.valueOf(0);
        for (int i=0;i<foodBeans.size();i++){
            account = account.add(foodBeans.get(i).getPrice().multiply(BigDecimal.valueOf(foodBeans.get(i).getSelectCount())));
            FoodListInfo foodListInfo = new FoodListInfo();
            listInfos.add(foodListInfo.setAmout(foodBeans.get(i).getSelectCount()).setCaipId(foodBeans.get(i).getId()));
        }

        tvSummary.setText(account.toString());
        tvShopName.setText(restaurantBean.getName());

        getPayWay();
    }

    private void getPayWay(){
        BaseRequest.getInstance().getApiServise().getPayWayList()
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new BaseObserver<List<PayWay>>(this) {
                    @Override
                    public void onSuccess(BaseReponse<List<PayWay>> baseReponse) {
                        List<PayWay> list = baseReponse.getResults();
                        payWays.clear();
                        payWays.addAll(list);
                        payTypeAdapter.notifyDataSetChanged();
                    }

                    @Override
                    public void onCodeError(BaseReponse tBaseReponse) {
                    }

                    @Override
                    public void onFailure(Throwable e, boolean netWork) throws Exception {

                    }
                });
    }

    @OnClick(R.id.tv_order)
    public void onViewClicked() {
        OrderPayInfo orderPayInfo = new OrderPayInfo()
                .setPayWay(payWays.get(payTypeAdapter.currentSelect).getValue())
                .setYingqId(restaurantBean.yingqId)
                .setShitId(restaurantBean.shitId)
                .setChuangkId(restaurantBean.getId())
                .setCustomerid(AppConfig.getString("userId"))
                .setMobile("")
                .setHtStOrderDetailList(listInfos);

        BaseRequest.getInstance().getApiServise().order(orderPayInfo)
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new BaseObserver<Map>(this) {
                    @Override
                    public void onSuccess(BaseReponse<Map> baseReponse) {
                        ToastUtils.showToast("下单成功");
                        listInfos.clear();
//                        finish();
                        showNormalDialog();
                    }

                    @Override
                    public void onCodeError(BaseReponse tBaseReponse) {
                    }

                    @Override
                    public void onFailure(Throwable e, boolean netWork) throws Exception {

                    }
                });
    }

    private void showNormalDialog(){
        /* @setIcon 设置对话框图标
         * @setTitle 设置对话框标题
         * @setMessage 设置对话框消息提示
         * setXXX方法返回Dialog对象，因此可以链式设置属性
         */
        final AlertDialog.Builder normalDialog =
                new AlertDialog.Builder(OrderedActivity.this);
        normalDialog.setTitle("下单成功");
        normalDialog.setMessage("您已经下单成功");
        normalDialog.setCancelable(false);
        normalDialog.setPositiveButton("确定",
                new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int which) {
                        OrderedActivity.this.finish();
                    }
                });
        // 显示
        normalDialog.show();
    }
}
